<?php



class gameReferee
{
        private $log;
        
        public function __construct($loger)
	{
		$this->log=$loger;
	}
        
        public function AddLog($sMessage,$title='gameReferee',$level=1)
        {
            if($this->log!=null)
                if($level==1)
                    $this->log->logSection($title,$sMessage);
        }
        
        
       public function changeStatusToEnd($oTournament)
       {
              
              $c=new Criteria();
              $c->add(GameMatchPeer::TOURNAMENT_ID,$oTournament->getId());
              $c->add(GameMatchPeer::STATUS,GameMatch::$GAMEMATCH_STATUS_START);
              $matches=GameMatchPeer::doSelect($c);
              
              $match_ids=array();
              
              foreach($matches as $match)
              {
                  $match_ids[]=$match->getId();
              }
              
              $this->AddLog('gramy: match-id',  var_dump($match_ids)); 
                            
              $c=new Criteria();
              $c->add(GamePeer::MATCH_ID,$match_ids,  Criteria::IN);
              $c->add(GamePeer::STATUS,Game::$GAME_STATUS_START);
              $games=GamePeer::doSelect($c);
              
              $games_ids=array();
              
              foreach($games as $game)
              {
                  $games_ids[]=$game->getId();
              }
              $this->AddLog('gramy: game-id',  var_dump($games_ids)); 
              
              
              $c = new Criteria();
              $c->addSelectColumn(ScorePeer::GAME_ID);
              $c->addSelectColumn('COUNT('.ScorePeer::SCORE.') as cnt');
              $c->add(ScorePeer::GAME_ID,$games_ids,Criteria::IN);
              $c->addGroupByColumn(ScorePeer::GAME_ID);
              $c->addHaving($c->getNewCriterion('cnt' ,2));
              $res=ScorePeer::doSelectStmt($c);
              
              //Zakanczanie game'ow
              
              $ranking=array();
              
              foreach ($res->fetchAll() as $node)
              {
                    $ranking[] = $node[0];
              }
              
              $match_ended=array();
              
              foreach($games as $game)
              {
                  if(in_array($game->getId(),$ranking))
                  {
                      $match_ended[]=$game->getMatchId();
                      $game->setStatus(Game::$GAME_STATUS_ENDED);
                      $game->save();
                  }
              }
              
              $this->AddLog( 'gramy: res',  var_dump($res));
             
              return $match_ended; 
       }
       
       public function EndGameResult($match_id,$type)
       {
           $c = new Criteria();
           $c->add(GameMatchPeer::ID,$match_id,  Criteria::IN);
           $matches=  GameMatchPeer::doSelect($c);
           
           $first_list=array();
           
           foreach ($matches as $match)
           {
               $first_list[]=$match->getFirstPlayerId();
           }
           
           $this->AddLog('EndGameResult<->$first_list: '.  var_dump($first_list));
           
           $c = new Criteria();
           $c->addSelectColumn(ScorePeer::MATCH_ID);
           $c->addSelectColumn('SUM('.ScorePeer::SCORE.') as cnt');
           $c->addSelectColumn(ScorePeer::PLAYER_ID);
           $c->add(ScorePeer::MATCH_ID,$match_id,Criteria::IN);
           $c->addGroupByColumn(ScorePeer::MATCH_ID);
           $c->addGroupByColumn(ScorePeer::PLAYER_ID);
           $res=ScorePeer::doSelectStmt($c);
                     
           $wins_first=array();
           $wins_second=array();
           $wins_draw=array();
           
           $tmp=array();
           
           if($type==gameRules::$GW_Greater)
           {
                foreach ($res->fetchAll() as $node)
                {
                    if(isset($tmp[$node[0]]))
                    {
                        if($tmp[$node[0]]>$node[1]){
                            if(in_array($node[2], $first_list))
                                    $wins_first[]=$node[0];
                            else
                                $wins_second[]=$node[0];
                        }else if($tmp[$node[0]]<$node[1]){
                                if(!in_array($node[2], $first_list))
                                    $wins_first[]=$node[0];
                            else
                                $wins_second[]=$node[0];
                        }else {
                            $wins_draw[]=$node[0];
                        }
                    }

                    $tmp[$node[0]] = $node[1];
                }
           }else{
               foreach ($res->fetchAll() as $node)
                {
                    if(isset($tmp[$node[0]]))
                    {
                        if($tmp[$node[0]]<$node[1]){
                            if(in_array($node[2], $first_list))
                                    $wins_first[]=$node[0];
                            else
                                $wins_second[]=$node[0];
                        }else if($tmp[$node[0]]>$node[1]){
                                if(!in_array($node[2], $first_list))
                                    $wins_first[]=$node[0];
                            else
                                $wins_second[]=$node[0];
                        }else {
                            $wins_draw[]=$node[0];
                        }
                    }

                    $tmp[$node[0]] = $node[1];
                }
           }
           
           $this->AddLog('EndGameResult<->array: '.  var_dump(array($wins_first,$wins_second,$wins_draw)));
           
           return array($wins_first,$wins_second,$wins_draw);
       }
       
       public function EndGame($matches,$match_id,$endValue)
       {
           $this->AddLog('EndGame<->entering...');
           
           $this->AddLog('EndGame<->$match_id: '.  var_dump($match_id));
           
           $c = new Criteria();
           $c->addSelectColumn(GamePeer::MATCH_ID);
           $c->addSelectColumn('COUNT('.GamePeer::ID.') as cnt');           
           $c->add(GamePeer::MATCH_ID,$match_id,Criteria::IN);
           $c->addGroupByColumn(GamePeer::MATCH_ID);
           $c->addHaving($c->getNewCriterion('cnt' ,$endValue));
           $res=ScorePeer::doSelectStmt($c);      
           
           $this->AddLog('EndGame<->ScorePeer::doSelectStmt: '.  var_dump($res));
           
           $ranking=array();
           
           foreach ($res->fetchAll() as $node)
           {
               $ranking[] = $node[0];
           }
           
           return $ranking;
           
       }
              
       public function EndPoints($matches,$match_id,$endValue,$type)
       {
           $this->AddLog('EndPoints: entering...');
           
           $this->AddLog('EndPoints<->$match_id: '.  var_dump($match_id));
           $c = new Criteria();
           $c->addSelectColumn(ScorePeer::MATCH_ID);
           $c->addSelectColumn('SUM('.ScorePeer::SCORE.') as cnt');
           $c->addSelectColumn(ScorePeer::PLAYER_ID);
           $c->add(ScorePeer::MATCH_ID,$match_id,Criteria::IN);
           $c->addGroupByColumn(ScorePeer::PLAYER_ID);
           
           if($type==gameRules::$GW_Greater)
            $c->addHaving($c->getNewCriterion('cnt' ,$endValue,  Criteria::GREATER_EQUAL));
           else
            $c->addHaving($c->getNewCriterion('cnt' ,$endValue,  Criteria::LESS_EQUAL));
           
           $res=ScorePeer::doSelectStmt($c);         
           
            $this->AddLog('EndPoints<->ScorePeer::doSelectStmt: '.  var_dump($res));
           
           $ranking=array();
           
           foreach ($res->fetchAll() as $node)
           {
              $ranking[] = $node[0];
           }
           
           return $ranking;          
           
       }
       
             
       public function EndDiffPoints($matches,$match_id,$endValue)
       {
           $this->AddLog('EndDiffPoints: entering...');
           
           $this->AddLog('EndDiffPoints<->$match_id: '.  var_dump($match_id));
           $c = new Criteria();
           $c->addSelectColumn(ScorePeer::MATCH_ID);
           $c->addSelectColumn('SUM('.ScorePeer::SCORE.') as cnt');
           $c->addSelectColumn(ScorePeer::PLAYER_ID);
           $c->add(ScorePeer::MATCH_ID,$match_id,Criteria::IN);
           $c->addGroupByColumn(ScorePeer::PLAYER_ID);
           $res=ScorePeer::doSelectStmt($c); 
           
           $ranking=array();
           
           $tmp=array();
           
           $this->AddLog('EndDiffPoints<->ScorePeer::doSelectStmt: '.  var_dump($res));
           
           foreach ($res->fetchAll() as $node)
           {
               if(isset($tmp[$node[0]]))
               {
                   if(abs($tmp[$node[0]]-$node[1])>=$endValue)
                    $ranking[] = $node[0];
               }
               
               $tmp[$node[0]] = $node[1];
           }
           
           return $ranking;
           
       }
       
       public function EndWinsResult($matches,$type)
       {
           $this->AddLog('EndWinsResult entering...');
           
           $wins_first=array();
           $wins_second=array();
           $wins_draw=array();
           
           $first_list=array();
           
            foreach ($matches as $match)
            {
                $first_list[]=$match->getFirstPlayerId();
            }
           
           foreach($matches as $match)
           {
                $c=new Criteria();
                $c->add(ScorePeer::MATCH_ID,$match->getId());
                $c->addDescendingOrderByColumn(ScorePeer::GAME_ID);
                $c->addDescendingOrderByColumn(ScorePeer::PLAYER_ID);
                $core=ScorePeer::doSelect($c);
                                    
                
                $players_win=array(array(0,0),array(0,0));                
                $res=array();
                
                if($type==gameRules::$GW_Less)
                {
                    
                    foreach($core as $co)
                    {
                        $this->AddLog('gameReferee: gameRules::$GW_Greater: '.$co->getPlayerId().':'.  $co->getScore());
                        
                        if(isset($res[$co->getGameId()]))
                        {
                            if($res[$co->getGameId()][1]>$co->getScore())
                            {
                                 $players_win[0][0]++;
                                 $players_win[0][1]=$co->getPlayerId();
                            }else if($res[$co->getGameId()][1]<$co->getScore())
                            {
                                 $players_win[1][0]++;
                                 $players_win[1][1]=$co->getPlayerId();
                            }
                        }                    
                        $res[$co->getGameId()]=array($co->getPlayerId(),$co->getScore());
                    }
                    
                    $this->AddLog('gameReferee: gameRules::$GW_Greater: '.  var_dump($players_win));
                    
                }else{
                    foreach($core as $co)
                    {
                        if(isset($res[$co->getGameId()]))
                        {
                            if($res[$co->getGameId()][1]<$co->getScore())
                            {
                                 $players_win[0][0]++;
                                 $players_win[0][1]=$co->getPlayerId();
                            }else if($res[$co->getGameId()][1]>$co->getScore())
                            {
                                 $players_win[1][0]++;
                                 $players_win[1][1]=$co->getPlayerId();
                            }
                        }                    
                        $res[$co->getGameId()]=array($co->getPlayerid(),$co->getScore());
                        $this->AddLog('gameReferee: gameRules::$GW_Greater: '.  var_dump($players_win));
                    }
                }               
                
                
                if($players_win[1][0]<$players_win[0][0]){
                    if(in_array($players_win[0][1], $first_list))
                        $wins_first[]=$match->getId();
                    else
                        $wins_second[]=$match->getId();
                }else if($players_win[1][0]>$players_win[0][0]){
                    if(!in_array($players_win[0][1], $first_list))
                         $wins_first[]=$match->getId();
                    else
                        $wins_second[]=$match->getId();
                }else{
                    $wins_draw[]=$match->getId();
                }
                              
                
           }
           
           return array($wins_draw,$wins_first,$wins_second);
       }
       
       public function EndWins($matches,$match_id,$endValue,$type)
       {
           $this->AddLog('EndWins entering...');
           
           $ranking=array();
           
           foreach($matches as $match)
           {
                $c=new Criteria();
                $c->add(ScorePeer::MATCH_ID,$match->getId());
                $c->addDescendingOrderByColumn(ScorePeer::GAME_ID);
                $c->addDescendingOrderByColumn(ScorePeer::PLAYER_ID);
                $core=ScorePeer::doSelect($c);
                
                 
                
                $players_win=array(0,0);                
                $res=array();
                
                if($type==gameRules::$GW_Less)
                {
                    
                    foreach($core as $co)
                    {
                        $this->AddLog('gameReferee: gameRules::$GW_Greater: '.$co->getPlayerId().':'.  $co->getScore());
                        
                        if(isset($res[$co->getGameId()]))
                        {
                            if($res[$co->getGameId()][1]>$co->getScore())
                            {
                                 $players_win[0]++;
                            }else if($res[$co->getGameId()][1]<$co->getScore())
                            {
                                 $players_win[1]++;
                            }
                        }                    
                        $res[$co->getGameId()]=array($co->getPlayerId(),$co->getScore());
                    }
                    
                    $this->AddLog('gameReferee: gameRules::$GW_Greater: '.  var_dump($players_win));
                    
                }else{
                    foreach($core as $co)
                    {
                        if(isset($res[$co->getGameId()]))
                        {
                            if($res[$co->getGameId()][1]<$co->getScore())
                            {
                                 $players_win[0]++;
                            }else if($res[$co->getGameId()][1]>$co->getScore())
                            {
                                 $players_win[1]++;
                            }
                        }                    
                        $res[$co->getGameId()]=array($co->getPlayerid(),$co->getScore());
                        $this->AddLog('gameReferee: gameRules::$GW_Greater: '.  var_dump($players_win));
                    }
                }
                
                foreach($players_win as $player)
                {
                    if($player==$endValue)
                        $ranking[]=$match->getId();
                }
           }
           
           return $ranking;           
   
       }
}